<?php
class db
{
    private $dbase;
    private $username = "";
    private $password = "";
    private $server = "";
    private $con = null;

    public $result = null;

    public function __construct($server, $username, $password) {
        $this->username = $username;
        $this->password = $password;
        $this->server = $server;
        $this->con = @mysql_connect($this->server, $this->username, $this->password);
        return $this->con;
    }

    public function dbSelect($dbase) {
        $this->dbase = $dbase;
        mysql_select_db($this->dbase, $this->con) or die("Can't connnect to database. " . mysql_error($this->con));
    }

    public function query($sql) {
        $this->setUtf8Code();
        $this->result = @mysql_query($sql, $this->con);
        if (!$this->result)
            die("Can\'t query data." . mysql_error($this->result));
        return $this->result;
    }

    public function queryshort($sql, $db) {
        $this->setUtf8Code();
        $this->dbSelect($db);
        $this->result = mysql_query($sql, $this->con);
        if (!$this->result)
            die("Can\'t query database" . mysql_error($this->result));

        return $this->result;
    }

    public function getCon() {
        return $this->con;
    }

    public function getObject()
    {
        return @mysql_fetch_object($this->result);
    }

    public function numRow()
    {
        return @mysql_num_rows($this->result) ;
    }

    public function affectedRow()
    {
        return @mysql_affected_rows($this->con);
    }

    public function free() {
        @mysql_free_result($this->result);
        mysql_close($this->con);
    }

    public function setUtf8Code() {
        @mysql_query('SET NAMES "utf8"');
    }

    public function getArrayObject()
    {
    	$objects = null;
    	while ($object = mysql_fetch_object($this->result)) {
    		$objects[] = $object ;
    	}

    	if (!empty($objects))
    	{
    		return $objects;
    	}
    	return false;
    }

    public function getLastId()
    {
    	$id = mysql_insert_id();
    	if(is_numeric($id))
    	{
			return $id;
    	}

    	return false;
    }
    
    public function startTrans()
    {
        @mysql_query("START TRANSACTION;");
    }
    
    public function commit()
    {
        @mysql_query("COMMIT;");
    }
    
    public function rollback()
    {
        @mysql_query("ROLLBACK;");
    }
}
?>